package com.ruoyi.controller;

import com.ruoyi.domain.common.PageResult;
import com.ruoyi.domain.common.ResponseResult;
import com.ruoyi.domain.common.UserHolder;
import com.ruoyi.domain.entity.Watchlist;
import com.ruoyi.domain.entity.WatchlistStockInfo;
import com.ruoyi.service.IWatchlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 自选股Controller
 *
 * @author ruoyi
 * @date 2025-01-13
 */
@RestController
@RequestMapping("/watchlist")
public class WatchlistController {
    @Autowired
    private IWatchlistService watchlistService;

    /**
     * 查询自选股列表
     */
//    @PreAuthorize("@ss.hasPermi('system:watchlist:list')")
    @GetMapping("/list")
    public ResponseResult list(Watchlist watchlist) {
        if (watchlist.getUserId() == null)
            return ResponseResult.error("未上传用户ID");
        Integer userId = UserHolder.getUserId();
        if (!watchlist.getUserId().equals(Long.valueOf(userId))) {
            return ResponseResult.error("用户ID异常");
        }
        List<Watchlist> list = watchlistService.selectWatchlistList(watchlist);
        PageResult<Watchlist> pageResult = new PageResult<>();
        pageResult.setTotal(list.size());
        pageResult.setRecords(list);
        return ResponseResult.success(pageResult);
    }

    /**
     * 获取自选股详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:watchlist:query')")
    @GetMapping(value = "/{id}")
    public ResponseResult getInfo(@PathVariable("id") Long id) {
        if (id == null)
            return ResponseResult.error("未上传用户ID");
        Integer userId = UserHolder.getUserId();
        if (!id.equals(Long.valueOf(userId))) {
            return ResponseResult.error("用户ID异常");
        }
        return ResponseResult.success(watchlistService.selectWatchlistById(id));
    }

    /**
     * 新增自选股
     */
//    @PreAuthorize("@ss.hasPermi('system:watchlist:add')")
//    @Log(title = "自选股", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseResult add(@RequestBody Watchlist watchlist) {
        return watchlistService.insertWatchlist(watchlist);
    }

    /**
     * 修改自选股
     */
//    @PreAuthorize("@ss.hasPermi('system:watchlist:edit')")
//    @Log(title = "自选股", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseResult edit(@RequestBody Watchlist watchlist) {
        return watchlistService.updateWatchlist(watchlist);
    }

    /**
     * 删除自选股
     */
//    @PreAuthorize("@ss.hasPermi('system:watchlist:remove')")
//    @Log(title = "自选股", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable Long id) {
        return watchlistService.deleteWatchlistById(id);
    }


    /**
     * 查询用户自选股相关的股票的基本信息
     */
    @PostMapping("/stockInfoList")
    public ResponseResult selectWatchlistStockInfoList(@RequestBody WatchlistStockInfo watchlistStockInfo) {
        if (watchlistStockInfo.getUserId() == null)
            return ResponseResult.error("未上传用户ID");
        Integer userId = UserHolder.getUserId();
        if (!watchlistStockInfo.getUserId().equals(Long.valueOf(userId))) {
            return ResponseResult.error("用户ID异常");
        }
        // 设置默认分页参数
        int pageNo = watchlistStockInfo.getPageNo() == null ? 1 : watchlistStockInfo.getPageNo();
        int pageSize = watchlistStockInfo.getPageSize() == null ? 20 : watchlistStockInfo.getPageSize();

        // 调用服务层方法获取分页数据
        PageResult<WatchlistStockInfo> pageResult = watchlistService.selectWatchlistStockInfoPage(watchlistStockInfo, pageNo, pageSize);

        return ResponseResult.success(pageResult);
    }

    /**
     * 判断股票是否在自选股中
     * @param stockId
     * @return
     */
    @GetMapping("/isInWatchlist")
    public ResponseResult isInWatchlist(@RequestParam("stockId") Long stockId) {
        Integer userId = UserHolder.getUserId();
        if (userId == null) {
            return ResponseResult.error("未登录");
        }
        Long watchlistID = watchlistService.isInWatchlist(Long.valueOf(userId), stockId);
        return ResponseResult.success(watchlistID);
    }
}
